package com.qxf.dao;

import java.sql.SQLException;

import org.apache.log4j.Logger;

import com.qxf.util.db.SQLProcessor;
import com.qxf.util.db.SQLUtil;

public class UserInfoDao {

	static Logger log = Logger.getLogger(UserInfoDao.class);
	
	public boolean addActivity(String activityName, String activityUrl, String userId) throws Exception {
		String sql = null;
		SQLProcessor sp = null;
		int flag = -1;
		try{
			sp = new SQLProcessor();
			sql = "insert into tb_activity(activity_name,activity_url,user_id) values(" +
					SQLUtil.toSqlString(activityName) + "," +
					SQLUtil.toSqlString(activityUrl) + "," +
					SQLUtil.toSqlInt(userId) +
					")";
			log.info("addActivity(): SQL=" + sql);
			flag = sp.executeUpdate(sql);
			if(flag == -1) {
				return false;
			}
		}catch(SQLException se){
			log.info("addActivity(): Error SQL=" + sql);
			sp.rollback();
			se.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			SQLProcessor.staticClose(sp);
		}
		return true;
	}
	
	public boolean saveActivity(String activityId, String activityName, String activityUrl, String userId) throws Exception {
		String sql = null;
		SQLProcessor sp = null;
		int flag = -1;
		try{
			sp = new SQLProcessor();
			sql = "update tb_activity set activity_name=" + SQLUtil.toSqlString(activityName) +
					", activity_url=" + SQLUtil.toSqlString(activityUrl) +
					" where activity_id=" + SQLUtil.toSqlInt(activityId) +
					"and user_id=" + SQLUtil.toSqlInt(userId);
			log.info("saveActivity(): SQL=" + sql);
			flag = sp.executeUpdate(sql);
			if(flag == -1) {
				return false;
			}
		}catch(SQLException se){
			log.info("saveActivity(): Error SQL=" + sql);
			sp.rollback();
			se.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			SQLProcessor.staticClose(sp);
		}
		return true;
	}
}
